﻿Public Class clsTesseract
    Public path As String = Application.StartupPath & "\Resources\Tesseract\"
    Private file_name As String
    Private TesseractInfo As ProcessStartInfo


    Public Sub New(ByVal file_name As String)
        Dim params As String = """" & path & file_name & ".png"" " & """" & path & file_name & """ phone"
        Me.file_name = file_name
        TesseractInfo = New ProcessStartInfo(path & "tesseract.exe", params)
        TesseractInfo.UseShellExecute = False
        TesseractInfo.RedirectStandardOutput = True
        TesseractInfo.CreateNoWindow = True
        'TesseractInfo.WindowStyle = ProcessWindowStyle.Minimized
    End Sub


    Public Sub ResizeBitmap(ByVal res_file_path As String)
        Dim bmp As Bitmap, w As Integer, h As Integer
        bmp = Image.FromFile(res_file_path)
        w = CInt(bmp.Size.Width * 3)
        h = CInt(bmp.Size.Height * 3)
        Dim bmp1 As New Bitmap(w + 20, h + 20)
        Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(bmp1)
        g.InterpolationMode = Drawing.Drawing2D.InterpolationMode.Default
        g.FillRectangle(Brushes.White, 0, 0, w + 20, h + 20)
        g.DrawImage(bmp, New Drawing.Rectangle(10, 10, w, h))
        g.Dispose()
        bmp.Dispose()
        bmp1.Save(path & file_name & ".png", System.Drawing.Imaging.ImageFormat.Png)
        bmp1.Dispose()
    End Sub


    Public Sub StartOCR()
        Dim TesseractProcess As New Process
        TesseractProcess.StartInfo = TesseractInfo
        TesseractProcess.Start()
        TesseractProcess.WaitForExit()
        'tmpS = TesseractProcess.StandardOutput.ReadToEnd
        TesseractProcess.Close()
    End Sub
End Class
